package com.xuxin.baby.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.xuxin.baby.R;
import com.xuxin.baby.activity.ReaderActivity;
import com.xuxin.baby.adapter.ReaderVideoAdapter;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.base.BaseFragment;
import com.xuxin.baby.bean.ReaderVideoBean;
import com.xuxin.baby.contract.ReaderVideoContract;
import com.xuxin.baby.presenter.ReaderVideoPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.RecycleUtils;

import butterknife.BindView;

/**
 * Created by xu_xin
 * Created on 2020.08.27 0027
 * 文章视频 -> 文章列表
 */
public class ReaderFragment extends BaseFragment implements ReaderVideoContract.View {
    private final ReaderVideoContract.Presenter presenter = new ReaderVideoPresenter(this);
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smart_refresh;
    @BindView(R.id.smart_recycle)
    RecyclerView smart_recycle;
    private ReaderVideoAdapter myAdapter;
    private String tenantId;//租户id
    private String token;//授权token
    private String categoryId;//分类id
    private String keywords;//搜索关键词
    private String ageRge;//年龄段
    private int pageNum = 0;

    public static ReaderFragment newInstance(String categoryId, String keywords, String ageRge) {
        ReaderFragment fragment = new ReaderFragment();
        Bundle bundle = new Bundle();
        bundle.putString("categoryId", categoryId);
        bundle.putString("ageRge", ageRge);
        bundle.putString("keywords", keywords);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View setContentView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.fragment_smart, container, false);
    }

    @Override
    public void initView() {
        //刷新加载
        smart_refresh.setEnableLoadMore(false);
        smart_refresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LogUtils.e("onRefresh");
                pageNum = 0;
                myAdapter.setList(null);
                handler.sendEmptyMessage(0);
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LogUtils.e("onLoadMore");
                handler.sendEmptyMessage(0);
            }
        });
        RecycleUtils.init(smart_recycle);
        myAdapter = new ReaderVideoAdapter();
        smart_recycle.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString(BaseConstant.readerId, myAdapter.getData().get(position).getId());
                ActivityUtils.startActivity(bundle, ReaderActivity.class);
            }
        });
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(getContext());
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        if (null != getArguments()) {
            categoryId = getArguments().getString("categoryId");
            ageRge = getArguments().getString("ageRge");
            keywords = getArguments().getString("keywords");
            smart_refresh.autoRefresh();
        }
    }

    @Override
    public void onComplete() {
        smart_refresh.finishRefresh();
        smart_refresh.finishLoadMore();
    }

    @Override
    public void onClick(View view) {

    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {//视频文章列表
                presenter.getReaderVideo(tenantId, token, categoryId, keywords, null, "common", ageRge, pageNum);
            }
            return false;
        }
    });


    @Override
    public void onSuccess(ReaderVideoBean readerVideoBean) {
        myAdapter.addData(readerVideoBean.getResult().getItems());
        if (readerVideoBean.getResult().getTotalCount() > myAdapter.getData().size()) {
            pageNum++;
            smart_refresh.setEnableLoadMore(true);
        } else {
            smart_refresh.setEnableLoadMore(false);
        }
    }

    public void searchKeywords(String keywords) {
        this.keywords = keywords;
        smart_refresh.autoRefresh();
    }
}